# coding=utf-8
from phosphorusScripts.bedcore.client import ClientSystem, clientApi, localPlayer
from phosphorusScripts.bedcore.common import cls2path
from phosphorusScripts.client.data import helper, manager
from phosphorusScripts.modInfo import ModInfo


class MainSystem(ClientSystem):

    def __init__(self, namespace, systemName):
        super(MainSystem, self).__init__(namespace, systemName)

    def addEventListen(self, namespace, systemName):
        self.ListenForEvent(namespace, systemName, 'LoadClientAddonScriptsAfter', self, self.LoadClientAddonScriptsAfter)
        self.ListenForEvent(namespace, systemName, 'UiInitFinished', self, self.UiInitFinished)
        self.ListenForEvent(ModInfo.NAME, 'main', 'OpenScreenEvent', self, self.OpenScreenEvent)
        self.ListenForEvent(ModInfo.NAME, 'main', 'OptionChangedEvent', self, self.OptionChangedEvent)
        self.ListenForEvent(ModInfo.NAME, 'main', 'OptionClickedEvent', self, self.OptionClickedEvent)

    def LoadClientAddonScriptsAfter(self, args):
        """
        :type args: dict
        """
        self.BroadcastEvent('PhosphorusLoadedEvent', {})
        from phosphorusScripts.client.ui.pauseScreenProxy import PauseScreenProxy
        instance = clientApi.GetNativeScreenManagerCls().instance()
        instance.RegisterScreenProxy('pause.pause_screen', cls2path(PauseScreenProxy))

    def UiInitFinished(self, args):
        """
        :type args: dict
        """
        from phosphorusScripts.client.ui.mainScreen import MainScreen
        clientApi.RegisterUI(ModInfo.NAME, 'main', cls2path(MainScreen), 'phosphorus.main_screen')
        from phosphorusScripts.client.ui.enumScreen import EnumScreen
        clientApi.RegisterUI(ModInfo.NAME, 'enum', cls2path(EnumScreen), 'phosphorus.enum_screen')

    def OpenScreenEvent(self, args):
        """
        :type args: dict
        """
        helper.isop = args['isop']
        helper.merge(args['server'], True)
        helper.merge(manager.getScreenData(), False)
        clientApi.PushScreen(ModInfo.NAME, 'main')

    def OptionChangedEvent(self, args):
        """
        :type args: dict
        """
        namespace, key = args['namespace'], args['key']
        if not manager.isModifiableOption(namespace, key):
            return
        if manager.getOptionOperator(namespace, key) and not helper.isop:
            return
        value = args['value']
        if not manager.setModifiableOptionCurrent(namespace, key, value):
            return
        data = {
            'namespace': namespace,
            'key': key,
            'value': value,
        }
        callback = manager.getOptionCallback(namespace, key)
        if callable(callback):
            callback(data)
        else:
            self.BroadcastEvent('ClientConfigChangedEvent', data)

    def OptionClickedEvent(self, args):
        """
        :type args: dict
        """
        namespace, key = args['namespace'], args['key']
        if not manager.isInteractiveOption(namespace, key):
            return
        if manager.getOptionOperator(namespace, key) and not helper.isop:
            return
        data = {
            'namespace': namespace,
            'key': key,
            'playerId': localPlayer,
        }
        callback = manager.getOptionCallback(namespace, key)
        if callable(callback):
            callback(data)
        else:
            self.BroadcastEvent('ClientButtonClickedEvent', data)
